package com.beici.vueblogapp.controller;

import com.beici.vueblogapp.utils.encryptionUtils;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @auther WaterGhost
 * @date 2021/10/31 22:20
 */

@RestController
@CrossOrigin
public class fileConftroller {
    // minio连接地址
    @Value("${MinIo.endpoint}")
    private  String endpoint;
    // minio连接key
    @Value("${MinIo.accessKey}")
    private  String accessKey;
    // minio连接密码
    @Value("${MinIo.secretKey}")
    private  String secretKey;
    // 存储内置博客内容图片桶（非封面）
    @Value("${MinIo.Buckets}")
    private String imagesBuckets;
    /**
     * 添加图片使用
     * @param file 上传的文件
     * @return  结果
     */
    @PostMapping("/uploadImage")
    public String uploadImage(MultipartFile file) {
         // 创建连接对象
        MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
        //  根据自己的算法计算出每张图片所存储的格式
        String encryption = encryptionUtils.minIoStorageEncryption(file.getOriginalFilename());
        try {
            minioClient.putObject(imagesBuckets, encryption,file.getInputStream(), new PutObjectOptions(file.getSize() ,-1));
            return encryption;
        }catch (ErrorResponseException e) {
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }
        return "添加失败";
    }

    @GetMapping("/downLoadImage/{FileName}")
    public void downLoadImage(@PathVariable String FileName, HttpServletResponse response){
        //  创建连接对象
        MinioClient minioClient = minioClient = new MinioClient(endpoint, accessKey, secretKey);;
        InputStream is = null;
        OutputStream os = null;
        try {
            is = minioClient.getObject(imagesBuckets, FileName);
            response.setContentType("image/jpeg");
            os = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int index =0 ;
            while ((index=is.read(bytes))!=-1){
                os.write(bytes,0,index);
            }
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }finally {
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
